package com.ebay.model;

import com.ebay.services.Bidding;
import com.ebay.services.Services;
import com.ebay.util.DBconn;

import java.sql.*;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Date;

public class Book {

	public int getSeller_id() {
		return seller_id;
	}

	public void setSeller_id(int seller_id) {
		this.seller_id = seller_id;
	}

	public Date getExpdate() {
		return expdate;
	}

	public void setExpdate(Date expdate) {
		this.expdate = expdate;
	}

	public int getProduct_id() {
		return product_id;
	}

	public void setProduct_id(int product_id) {
		this.product_id = product_id;
	}

	public int getCategory_id() {
		return category_id;
	}

	public void setCategory_id(int category_id) {
		this.category_id = category_id;
	}

	public boolean getBiddingstatus() {
		return biddingstatus;
	}

	public void setBiddingstatus(boolean biddingstatus) {
		this.biddingstatus = biddingstatus;
	}

	public int getSubcategory_id() {
		return subcategory_id;
	}

	public void setSubcategory_id(int subcategory_id) {
		this.subcategory_id = subcategory_id;
	}

	public int getproduct_id() {
		return product_id;
	}

	public void setproduct_id(int product_id) {
		this.product_id = product_id;
	}

	public float getSubtotal() {
		return subtotal;
	}

	public void setSubtotal(float subtotal) {
		this.subtotal = subtotal;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public String getCond() {
		return cond;
	}

	public void setCond(String cond) {
		this.cond = cond;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

	public String getFormat_type() {
		return format_type;
	}

	public void setFormat_type(String format_type) {
		this.format_type = format_type;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	private int category_id;
	private boolean biddingstatus;
	private int subcategory_id;
	private int product_id;
	private float subtotal;
	private String title;
	private String description;
	private float price;
	private String cond;
	private int pages;
	private String author;
	private String publisher;
	private String image;
	private String lang;
	private String format_type;
	private int quantity;
	private int user_id;
	private Date expdate;
	private int seller_id;
	private Bidding bid;
	private float shippingCharges;

	public float getShippingCharges() {
		return shippingCharges;
	}

	public void setShippingCharges(float shippingCharges) {
		this.shippingCharges = shippingCharges;
	}

	public Bidding getBid() {
		return bid;
	}

	public void setBid(Bidding bid) {
		this.bid = bid;
	}

	public String getPriceText(){
		DecimalFormat df = new DecimalFormat("###.00");
		return df.format((double)price);
	}

	public String getShippingChargesText(){
		DecimalFormat df = new DecimalFormat("###.00");
		return df.format((double)shippingCharges);
	}

	public String getTimeRemaining(){
		return Services.getEndTimeText(expdate);
	}

	public Book(){shippingCharges = 0;}

	public Book(int category_id, int subcategory_id, int product_id,
			String title, String description, Float price, String cond,
			int pages, String author, String publisher, String lang,
			String format_type, String image, boolean biddingstatus,
			int quantity,Date expdate,int seller_id) {
		this.category_id = category_id;
		this.subcategory_id = subcategory_id;
		this.product_id = product_id;
		this.title = title;
		this.description = description;
		this.price = price;
		this.cond = cond;
		this.pages = pages;
		this.author = author;
		this.publisher = publisher;
		this.lang = lang;
		this.format_type = format_type;
		this.image = image;
		this.biddingstatus = biddingstatus;
		this.quantity = quantity;
		this.expdate = expdate;
		this.seller_id = seller_id;
	}

	public String toString(){
		String str = null;
		str +="\n Category ID : " + category_id;
		str +="\n SubCAT ID : " + subcategory_id;
		str +="\n Pdct ID : " + product_id;
		str +="\n Title : " + title;
		str +="\n Bidding Status : " + biddingstatus;
		return str;
	}

	public static int getBookList(String query,LinkedHashMap<String, Object> lstbooks){
		Connection connection = null;
		ResultSet rs = null;

		query = "select SQL_CALC_FOUND_ROWS * from bookdetails where status = 1"+query+";";
		System.out.println("Query : "+query);
		try {
			connection=DBconn.getConnection();
			Statement stmt=connection.createStatement();

			rs=stmt.executeQuery(query);
			while (rs.next()) {
				int category_id=rs.getInt("category_id");
				int subcategory_id=rs.getInt("subcategory_id");
				int product_id=rs.getInt("product_id");
				String title=rs.getString("title");
				String description=rs.getString("description");
				Float price=rs.getFloat("price");
				String cond=rs.getString("cond");
				int pages=rs.getInt("pages");
				String author=rs.getString("author");
				String publisher=rs.getString("publisher");
				String lang=rs.getString("lang");
				String format_type=rs.getString("format_type");
				String image=rs.getString("image");
				int biddingstatus=rs.getInt("biddingstatus");
				//				int bid_id=rs.getInt("bid_id");
				int quantity=rs.getInt("quantity");
				Date expdate = rs.getTimestamp("expdate");
				int seller_id = rs.getInt("seller_id");

				Book book=new Book(category_id,subcategory_id,product_id,title,description,price,cond,pages,author,publisher,lang,format_type,image,(biddingstatus==1)?true:false,quantity,expdate,seller_id);
				if(biddingstatus==1){
					Bidding bid = new Bidding(category_id, subcategory_id, product_id);
					bid.getBiddingDetails();
					book.setBid(bid);
					book.setPrice(bid.getBidPrice());
				}
				System.out.println(book.toString());
				lstbooks.put(author+" | "+lang+" | "+format_type,book);
			}
			query = "SELECT FOUND_ROWS();";
			rs = stmt.executeQuery(query);
			while (rs.next()) {
				return rs.getInt(1);
			}
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());

		}
		return -1;
	}




	public static Book getBookDetails(int product_id) {
		Book book = null;
		PreparedStatement prepStmt = null;
		try {
			Connection connection=null;
			connection=DBconn.getConnection();
			ResultSet rs = null;			
			String QueryString = "select * from bookdetails where product_id=?";
			prepStmt = connection.prepareStatement(QueryString);
			prepStmt.setInt(1, product_id);

			rs=prepStmt.executeQuery();

			while (rs.next()) {
				int category_id=rs.getInt("category_id");
				int subcategory_id=rs.getInt("subcategory_id");
				product_id=rs.getInt("product_id");
				String title=rs.getString("title");
				String description=rs.getString("description");
				Float price=rs.getFloat("price");
				String cond=rs.getString("cond");
				int pages=rs.getInt("pages");
				String author=rs.getString("author");
				String publisher=rs.getString("publisher");
				String lang=rs.getString("lang");
				String format_type=rs.getString("format_type");
				String image=rs.getString("image");
				Date expdate = rs.getTimestamp("expdate");
				System.out.println("Image : "+image);
				int biddingstatus=rs.getInt("biddingstatus");
				int seller_id = rs.getInt("seller_id");

				//				int bid_id=rs.getInt("bid_id");
				int quantity=rs.getInt("quantity");
				book=new Book(category_id,subcategory_id,product_id,title,description,price,cond,pages,author,publisher,lang,format_type,image,biddingstatus==1?true:false,quantity,expdate,seller_id);
				System.out.println(book.toString());
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return book;
	}

	public boolean isExpired() {
		Bidding bid = new Bidding(category_id, subcategory_id, product_id);
		bid.getBiddingDetails();
		Date now = new Date();
		if(bid.getEndTime().before(now))
			return true;
		return false;
	}
}
